<template>
  <div>
     <el-card class="box-card">
    <div class="header" slot="header"><span>城市列表数据添加</span></div>  
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            
       
      <el-form-item label="城市名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入城市名称" autofocus></el-input>
      </el-form-item>
         <el-form-item label="城市拼音" prop="pinyin">
        <el-input v-model="form.pinyin" placeholder="请输入城市拼音"></el-input>
      </el-form-item>
  
      <el-form-item label="是否热门">
        <el-switch v-model="form.isHot" :active-text="form.isHot?'是':'否'"></el-switch>
      </el-form-item>   
    
    
      <el-form-item>
        <el-button type="primary" :loading="loading" @click="onSubmit">立即创建</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
     </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      form: {
          name:"",
          pinyin:"",
        isHot:false,      
      },
         rules: {
          name: [
            { required: true, message: '请输入城市名称', trigger: 'blur' },           
          ],
          pinyin:[
            {
              required: true, message: '请输入城市拼音', trigger: 'blur'
            }
          ]
          },
       loading:false,
    };
  },
  methods: {
    onSubmit() {
        this.loading = true;
        let url = "/city/add"
        this.$axios.post(url,this.form).then(res=>{
            this.loading=false
            console.log("res",res)
             this.$message.success("添加成功");
             this.form={}
        }).catch(err=>{
            this.loading=false
            console.log("失败",err)
              this.$message.error("添加失败");
        })
        console.log("this.loading",this.loading)
      console.log("submit!",this.form);
    }
  }
};
</script>> 

<style lang="less" scoped>
    .header{
      // margin-top: 100px;
    font-size: 22px;
    display: flex;
    // justify-content: center;
    align-items: center;   
    }
    .el-input{
      width: 500px;
    }
    .box-card {
  width: 100%;
  height: 85vh;
}

</style>